package com.aglow.bluetoothspeaker.main.base;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;


import com.aglow.bluetoothspeaker.R;
import com.aglow.bluetoothspeaker.main.ui.activity.MainActivity;
import com.aglow.bluetoothspeaker.main.ui.dialog.PromptDialogV2;
import com.aglow.bluetoothspeaker.model.event.ConnectedStateChangedEvent;
import com.aglow.bluetoothspeaker.model.event.HomeKeyEvent;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;


/**
 * chufeng
 * 2018/9/4
 * **/
public abstract class BaseActivity extends AppCompatActivity implements IActivity {
    private Unbinder unbinder;
    public Activity mContext;
    protected boolean isActivityForeground = false;
    protected boolean isRegisterEventBus = true;
    public PromptDialogV2 disconnectHintDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        mContext = this;
        this.initData();
        this.initView();
        this.addViewListener();
        getPremissions();
        if (isRegisterEventBus) {
            EventBus.getDefault().register(this);
        }
    }

    @SuppressWarnings({"unused"})
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onConnectionStateChanged(ConnectedStateChangedEvent event) {
        Log.i("base","event = "+event);
        if (!event.isConnected()) {
            showBluetoothDisconnectDialog();
        }else {
            Log.i("base","撤销对话框");
            if (disconnectHintDialog!=null){
                disconnectHintDialog.dismiss();
            }

        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        onBackClick();
    }

    public void showBluetoothDisconnectDialog() {
        if (!isActivityForeground) {
            return;
        }
        if (disconnectHintDialog == null) {
            disconnectHintDialog = new PromptDialogV2(mContext);
            disconnectHintDialog.setOnButtonClickListener(new PromptDialogV2.OnButtonClickListener() {
                @Override
                public void onPositiveClick() {
                    disconnectHintDialog.dismiss();
                }

                @Override
                public void onNegativeClick() {
                    MainActivity.showActivity(mContext, true);
                    disconnectHintDialog.dismiss();
                }
            });
        }
        disconnectHintDialog.show();

    }



    /**
     * 动态申请权限
     * **/
    public void getPremissions(){

        XXPermissions.with(this).permission(Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_LOGS, Manifest.permission.KILL_BACKGROUND_PROCESSES,
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION).request(new OnPermission() {
            @Override
            public void hasPermission(List<String> list, boolean b) {

            }

            @Override
            public void noPermission(List<String> list, boolean b) {

            }
        });
    }


    /**
     * 布局资源 id
     *
     * @return 资源id
     */
    public abstract int getLayoutId();


    /**
     * 带返回的跳转
     *
     * @param cls         class
     * @param extras      参数
     * @param requestCode 请求码
     */
    @SuppressWarnings("unused")
    public void showActivityForResult(Class<?> cls, Bundle extras, int requestCode) {
        Intent intent = new Intent();
        intent.putExtras(extras);
        intent.setClass(this, cls);
        startActivityForResult(intent, requestCode);
    }
    @Override
    protected void onStart() {
        super.onStart();
//        if (isRegisterEventBus) {
//            EventBus.getDefault().register(this);
//        }
        isActivityForeground = true;
    }


    @Override
    protected void onStop() {
        super.onStop();
//        if (isRegisterEventBus) {
//            EventBus.getDefault().unregister(this);
//        }
        isActivityForeground = false;
    }

    public void showActivity(Class<?> cls) {
        Intent intent = new Intent(mContext, cls);
        startActivity(intent);
    }

    public abstract void onBackClick();



    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
        if (isRegisterEventBus) {
            EventBus.getDefault().unregister(this);
        }
    }
}
